package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.AddressDao;
import com.itheima.dao.OrderDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Address;
import com.itheima.pojo.Order;
import com.itheima.service.AddressService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = AddressService.class)
@Transactional
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressDao addressDao;

    //新建
    @Override
    public void add(Address address) {
        addressDao.add(address);
    }

    //查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        if(queryPageBean != null){
            //获得数据
            Integer currentPage = queryPageBean.getCurrentPage();
            Integer pageSize = queryPageBean.getPageSize();
            //设置分页
            PageHelper.startPage(currentPage,pageSize);

            Page page = addressDao.findPage(queryPageBean.getQueryString());
            if(page != null){
                //获得总数和数据的集合,返回controller层
                long total = page.getTotal();
                List list = page.getResult();

                return new PageResult(total,list);
            }

        }
        return null;
    }

    //根据id查询
    @Override
    public Address findById(Integer id) {
        Address address = addressDao.findById(id);
        return address;
    }

    //编辑
    @Override
    public void update(Address address) {
        addressDao.update(address);
    }

    //删除
    @Override
    public void delete(Integer id) {
        addressDao.delete(id);
    }

    //根据id查找order
    @Override
    public List<Map> findOrderByAddressId(Integer addressId) throws Exception {

        //查询所有order
        List<Order> orders = addressDao.findOrderByAddressId(addressId);
        List<Map> list = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        for (Order order : orders) {
            //根据id查找member姓名
            String memberName = addressDao.findNameByMemberId(order.getMemberId());
            String setmealName = addressDao.findNameBySetmealName(order.getSetmealId());

            map.put("memberName",memberName);
            map.put("setmealName",setmealName);
            map.put("orderDate", DateUtils.parseDate2String(order.getOrderDate()));
            map.put("orderType",order.getOrderType());
            map.put("orderStatus",order.getOrderStatus());
            list.add(map);
        }
        return list;
    }

    //查找所有address
    @Override
    public List<Map> findAllAddress() {

        List<Address> addresses = addressDao.findAllAddress();

        List<Map> list = new ArrayList<>();


        for (Address address : addresses) {
            Map<String, String> map = new HashMap<>();
            map.put("value",address.getLngAndLat());
            map.put("label",address.getAddress());
            list.add(map);
        }
        return list;
    }
}
